#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[20005];

signed main() {
	int n, sum = 0;
	cin >> n;

	priority_queue<int, vector<int>, greater<int>> q;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		q.push(a[i]);
	}

	while (q.size() != 1) {
		int t = 0;
		t += q.top();
		q.pop();
		t += q.top();
		q.pop();
		
		sum += t;
		q.push(t);
	}
	cout << sum;
	return 0;
}
